package com.lm56.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ajax请求session超时拦截处理 只针对ajax进行处理 <一句话功能简述> <功能详细描述>
 * 
 * @author 卢明明
 * @version [版本号, 2013-9-12 下午7:11:36]
 */
public class AjaxSessionTimeoutFilter implements Filter {

	private static Logger logger = LoggerFactory.getLogger(AjaxSessionTimeoutFilter.class);


	public void init(FilterConfig filterConfig) throws ServletException {
	}

	/**
	 * 只针对ajax进行处理
	 * 
	 * @param request
	 * @param response
	 * @param chain
	 * @throws IOException
	 * @throws ServletException
	 */


	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		// 判断session里是否有用户信息,如果没有用户信息则认为session过期 
//		if (req.getSession().getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
//
//			//判断移动应用则不做session 超时
//			if(Pattern.matches("/bas/MsgHttpService/.*", req.getRequestURI())||Pattern.matches("/bas/interface/.*", req.getRequestURI())||Pattern.matches("/bas/mobi/.*", req.getRequestURI())||(Pattern.matches("/bas/login", req.getRequestURI())&&req.getHeader("x-requested-with") != null && req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest"))){
//				//放开移动端的ajax的过滤
//				chain.doFilter(req, res);
//			} else
//
//			// 如果是ajax请求响应头会有，x-requested-with；
//			// 如果是安全地址不进行处理　
//			if (req.getHeader("x-requested-with") != null && req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
//				logger.warn("ajax session timeout!");
//				res.setStatus(911);// 表示session timeout
//			} else {
//				chain.doFilter(req, res);
//			}
//		} else {
//			chain.doFilter(req, res);
//		}

	}

	public void destroy() {
	}

}
